package cn.cloudplug.friendsrecommend;

import java.io.FileOutputStream;

import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.util.ShareUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

public class MyScoreActivity extends Activity {

	private WebView webView;
	String baseUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_score);
		setView();
	}

	private void setView() {
		
		/*
		 * findViewById(R.id.iv_share).setOnClickListener(new OnClickListener()
		 * { public void onClick(View v) { PopupMenu popup=new
		 * PopupMenu(MyScoreActivity.this, v);
		 * popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
		 * popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
		 * public boolean onMenuItemClick(MenuItem item) { switch
		 * (item.getItemId()) { case R.id.action_share_race: ShareUtil share=new
		 * ShareUtil(MyScoreActivity.this); share.Share(); return true; case
		 * R.id.action_focus_race: Toast.makeText(MyScoreActivity.this,
		 * "关注赛事",Toast.LENGTH_SHORT).show(); return true; case
		 * R.id.action_scan: startActivity(new
		 * Intent(MyScoreActivity.this,QRCodeActivity.class)); return true; case
		 * R.id.action_report: Toast.makeText(MyScoreActivity.this,
		 * "举报",Toast.LENGTH_SHORT).show(); return true; } return false; } });
		 * popup.show(); } });
		 */
//		webView = (WebView) findViewById(R.id.wv_my_score);
//		webView.loadUrl("http://www.baidu.com");
//		webView.setWebViewClient(new HelloWebViewClient());
//		webView.setOnKeyListener(new View.OnKeyListener() {
//			@Override
//			public boolean onKey(View v, int keyCode, KeyEvent event) {
//				if (event.getAction() == KeyEvent.ACTION_DOWN) {
//					if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
//						baseUrl = webView.getOriginalUrl();
//						webView.goBack(); // 后退
//						if (baseUrl.equals(webView.getOriginalUrl())) {
//							MyScoreActivity.this.finish();
//							return false;
//						}
//					}
//					return true; // 已处理
//				}
//				return false;
//
//			}
//		});
		ViewGroup title = (ViewGroup) findViewById(R.id.title);
		title.findViewById(R.id.iv_more).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						PopupMenu popup = new PopupMenu(MyScoreActivity.this, v);
						popup.getMenuInflater().inflate(R.menu.main,
								popup.getMenu());
						popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
							public boolean onMenuItemClick(MenuItem item) {
								switch (item.getItemId()) {
								case R.id.action_share_race:
									getPicture();
									share();
									return true;
								case R.id.action_focus_race:
									Toast.makeText(MyScoreActivity.this,
											"关注赛事", Toast.LENGTH_SHORT).show();
									return true;
								case R.id.action_scan:
									startActivity(new Intent(
											MyScoreActivity.this,
											QRCodeActivity.class));
									return true;
								case R.id.action_report:
									Toast.makeText(MyScoreActivity.this, "举报",
											Toast.LENGTH_SHORT).show();
									return true;
								}
								return false;
							}

							private void share() {
								// TODO Auto-generated method stub
								ShareUtil share=new ShareUtil(MyScoreActivity.this);
								share.share(baseUrl);
							}
						});
						popup.show();
					}
				});
	}

	public void getPicture() {
		Picture picture=webView.capturePicture();
		 int width = picture.getWidth();  
         int height = picture.getHeight();  
         if (width > 0 && height > 0) {  
             //创建指定高宽的Bitmap对象  
             Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
             //创建Canvas,并以bitmap为绘制目标  
             Canvas canvas = new Canvas(bitmap);  
             //将WebView影像绘制在Canvas上  
             picture.draw(canvas);  
             try {  
                 String filePath = Environment.getExternalStorageDirectory()+"/sharePicture.jpg";  
                 FileOutputStream fos = new FileOutputStream(filePath);  
                 //压缩bitmap到输出流中  
                 bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);  
                 fos.close();  
                 Toast.makeText(MyScoreActivity.this, "CAPTURE SUCCESS", Toast.LENGTH_LONG).show();  
             } catch (Exception e) {  
                 Log.e("TAG", e.getMessage());  
             }  
         }
	}

	// Web视图
	private class HelloWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

}
